#include <buola/scene/techniques/cmeshtechnique.h>
#include <buola/scene/crendercontext.h>

#include "shaders/sVertexShader.h"
#include "shaders/sFragmentShader.h"

namespace buola { namespace scene {

CMeshTechnique::CMeshTechnique()
{
    mProgram.CreateAndAttach(shaders::sVertexShader,shaders::sFragmentShader);
    mProgram.BindFragmentData(0,"oFragColor");
    mProgram.BindAttribLocation(0,"iVertex");
    mProgram.BindAttribLocation(1,"iColor");
    mProgram.BindAttribLocation(2,"iNormal");
    mProgram.Link();

    uMVMatrix=mProgram.GetUniform("uMVMatrix");
    uMVPMatrix=mProgram.GetUniform("uMVPMatrix");
}

CMeshTechnique::~CMeshTechnique()
{
    
}

void CMeshTechnique::Prepare(CRenderContext &pContext)
{
    if(pContext.mTechnique!=this)
    {
        mProgram.Use();

        pContext.mTechnique=this;
    }

    mat::CMat34d lMVMatrix=pContext.mViewMatrix*pContext.mModelMatrix;
    uMVMatrix.SetValue(lMVMatrix);
    uMVPMatrix.SetValue(pContext.mProjectionMatrix*lMVMatrix);
    glVertexAttrib3d(1,0.5,0.5,0.5);
}

CMeshTechnique *CMeshTechnique::Get()
{
    static CMeshTechnique *lTechnique=new CMeshTechnique;
    return lTechnique;
}

/*namespace scene*/ } /*namespace buola*/ }